package com.mkr.servlet;

import com.mkr.pojo.Profile;
import com.mkr.service.ProfileService;
import com.mkr.service.impl.ProfileServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author mkr
 * @version V1.0
 * @Project BookApp
 * @Package ${PACKAGE_NAME}
 * @Description: ${TODO}
 * @Date 2022/5/14 19:24
 */
@WebServlet("/addProfile")
public class AddProfileServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();

        //获取参数
        //获取参数
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String birthday = request.getParameter("birthday");
        String gender = request.getParameter("gender");
        Double carder = Double.valueOf(request.getParameter("carder"));
        String address = request.getParameter("address");
        String mobile = request.getParameter("mobile");
        String picture = request.getParameter("picture");

        Profile profile = new Profile(id, name, birthday, gender, carder, address, mobile, picture);
        //调用业务层，将书籍添加到数据库中
        ProfileService profileService = new ProfileServiceImpl();
        boolean result = profileService.addProfile(profile);

        if (result){
            //添加成功，显示新的列表
            response.sendRedirect(request.getContextPath()+"/userList");
        }else {
            //添加失败
            session.setAttribute("mag", "添加失败");
            response.sendRedirect(request.getContextPath()+"/addProdile.jsp");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
